<template>
  <div class="ATab" ref="ATab">
    <div class="icons" @click="handLeft" v-if="showIcons">
      <i class="el-icon-arrow-left"></i>
    </div>
    <ul ref="menuTabs">
      <li
        v-for="(item, index) in menuTabs"
        :class="{ active: item.route === $route.name }"
        :key="index"
        @contextmenu.prevent="handleContextMenu(index)"
        @click="handTab(item)"
      >
        {{ item.name }}
        <div class="close" @click="handClose(item)">
          <i class="el-icon-close"></i>
        </div>
        <div class="lists" @mouseleave="handleMouseLeave" v-if="activeTab === index">
          <div class="sanjiao"></div>
          <div class="list" @click="handClose(item)">关闭当前</div>
          <div class="list" @click="handCloseAll()">关闭所有</div>
          <div class="list" @click="handCloseOther(item)">关闭其他</div>
        </div>
      </li>
    </ul>
    <div class="icons" @click="handRight" v-if="showIcons">
      <i class="el-icon-arrow-right"></i>
    </div>
  </div>
</template>
<script>
import { mapActions } from 'vuex';
export default {
  props: {},
  data() {
    return {
      showIcons: false,
      distance: 100,
      activeTab: '',
    };
  },
  components: {},
  watch: {
    $route() {
      this.isScroll();
    },
  },
  computed: {
    menuTabs() {
      return this.$store.state.state.menuTabs; // 返回$store对象的状态值
    },
  },
  created() {},
  mounted() {
    this.isScroll();
  },
  methods: {
    ...mapActions('state', ['updateMenuTabsAsync']),
    // 关闭
    handClose(val) {
      if (this.menuTabs.length === 1) {
        if (val.route !== 'stoneManage') {
          this.handUpadtaMenuTabs(val);
          this.$router.push('/' + 'stoneManage');
        }
      } else {
        if (val.route === this.$route.name) {
          this.handUpadtaMenuTabs(val);
          this.$router.push('/' + 'stoneManage');
        } else {
          this.handUpadtaMenuTabs(val);
        }
      }
      this.handleMouseLeave();
    },
    // 过滤menuTabs
    handUpadtaMenuTabs(val) {
      const arr = this.menuTabs.filter((item) => item.route !== val.route);
      this.updateMenuTabsAsync(arr);
    },
    // 关闭所有
    handCloseAll() {
      const arr = [
        {
          route: 'stoneManage',
          name: '砂石料场管理',
        },
      ];
      this.updateMenuTabsAsync(arr);
      this.$router.push('/' + 'home');
    },
    // 关闭其他
    handCloseOther(val) {
      const arr = [val];
      if (val.route !== this.$route.name) {
        this.updateMenuTabsAsync(arr);
        this.$router.push('/' + val.route);
      }
    },
    // 初始化是否有滚动
    isScroll() {
      this.$nextTick(() => {
        if (this.$refs.menuTabs.clientWidth + 100 > this.$refs.ATab.scrollWidth) {
          this.showIcons = true;
        } else {
          this.showIcons = false;
        }
      });
    },
    // 移动
    handLeft() {
      // 可滚动距离
      if (this.$refs.menuTabs.scrollLeft > 0) {
        this.$refs.menuTabs.scrollLeft -= this.distance;
      }
    },
    handRight() {
      // 可滚动距离
      var scrollWidth = this.$refs.menuTabs.scrollWidth - this.$refs.menuTabs.clientWidth;
      if (this.$refs.menuTabs.scrollLeft < scrollWidth) {
        this.$refs.menuTabs.scrollLeft += this.distance;
      }
    },
    // 点击右键
    handleContextMenu(index) {
      this.activeTab = index;
    },
    // 离开
    handleMouseLeave() {
      this.activeTab = '';
    },
    // 点击跳转
    handTab(val) {
      this.$router.push('/' + val.route);
    },
  },
};
</script>
<style lang="scss" scoped>
.ATab {
  height: 30px;
  width: 100%;
  background-color: #fff;
  display: flex;

  .icons {
    font-size: 15px;
    line-height: 30px;
    margin-left: 10px;
    text-align: center;
    cursor: pointer;
  }

  ul {
    display: flex;
    overflow-x: scroll;
    height: 30px;
    padding-right: 20px;
    li {
      height: 24px;
      line-height: 24px;
      padding: 0 10px;
      margin: 0 5px;
      background-color: #f1f1f1;
      margin-top: 3px;
      color: #666666;
      border-radius: 5px;
      font-size: 12px;
      cursor: pointer;
      display: flex;
      flex-shrink: 0;

      .close {
        position: relative;
        margin-left: 10px;
        width: 16px;
        height: 16px;
        margin-top: 4px;
        border-radius: 50%;
        line-height: 16px;
        text-align: center;
        display: none;
      }

      &:hover {
        .close {
          display: block;
        }
      }
    }
    .active {
      color: #fff;
      background-color: #57a9ff;
    }

    &::-webkit-scrollbar {
      display: none; /* Chrome Safari */
    }
  }

  .lists {
    position: absolute;
    top: 90px;
    color: #666666;
    background-color: #fff;
    border-radius: 5px;
    z-index: 1;
    .sanjiao {
      width: 0;
      height: 0;
      border-left: 5px solid transparent; /* 左边透明 */
      border-right: 5px solid transparent; /* 右边透明 */
      border-top: 8px solid #fff; /* 顶部颜色 */
      transform: rotate(180deg); /* 旋转90度 */
      position: relative;
      top: -8px;
      left: 10px;
    }
    .list {
      padding: 5px 10px;
      cursor: pointer;
      &:hover {
        color: #57a9ff;
      }
    }
  }
}
</style>
